package com.gitee.leijin.rbac.config.properties;


import cn.hutool.core.util.StrUtil;
import com.gitee.leijin.rbac.constants.CommonConstants;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;

@Slf4j
@Data
@ConfigurationProperties(prefix = "upload")
public class UploadProperties {

    private String staticDir;

    @PostConstruct
    public void init() {
        if (StrUtil.isBlank(staticDir)) {
            throw new RuntimeException("UploadProperties.staticDir未配置");
        }
        if (!staticDir.endsWith(CommonConstants.SLASH)) {
            throw new RuntimeException("UploadProperties.staticDir没有以/结尾");
        }
        File tmp = new File(staticDir);
        if (!tmp.isAbsolute()) {
            throw new RuntimeException("UploadProperties.staticDir不是绝对路径");
        }
        if (!tmp.exists()) {
            try {
                FileUtils.forceMkdir(tmp);
            } catch (IOException e) {
                log.error("创建上传目录出错", e);
                throw new RuntimeException(e);
            }
        }
    }
}
